/**
 * Copyright (C) 2009 Noel Ang
 *
 * This file is part of Samsara.
 *
 * Samsara is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Samsara is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Samsara.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package com.lunarshades.samsara.ui.actions;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.net.URL;
import java.text.MessageFormat;
import javax.swing.Action;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import com.lunarshades.samsara.Raider;
import com.lunarshades.samsara.ui.RosterTableModel;
import com.lunarshades.samsara.ui.SamsaraFrame;
import static com.lunarshades.samsara.ui.actions.RosterChangeEvent.RosterChangeType;

/**
 * Action for adding new entries to a RosterTableModel.
 *
 * @author Noel Ang, sparta.variable@gmail.com
 * @see com.lunarshades.samsara.ui.RosterTableModel
 */
public class RosterAddAction<E extends Raider> extends RosterChangeAction {

public RosterAddAction(RosterTableModel<E> model,
                       Class<E> elementClass,
                       SamsaraFrame pAppFrame
) {
    super(NAME);

    URL smallIconUrl = getClass().getResource(
            "/com/lunarshades/samsara/ui/icons/add16.gif"
    );
    putValue(Action.SMALL_ICON, new ImageIcon(smallIconUrl));
    putValue(Action.SHORT_DESCRIPTION, SHORT_TEXT);
    putValue(Action.MNEMONIC_KEY, KeyEvent.VK_A);
    mModel = model;
    mElementClass = elementClass;
    mApp = pAppFrame;
}

public void actionPerformed(ActionEvent e) {
    String name = JOptionPane.showInputDialog(mApp,
            "Name:",
            SHORT_TEXT,
            JOptionPane.QUESTION_MESSAGE
    );

    if (name != null && !"".equals(name.trim())) {
        E raider = null;
        try {
            raider = mElementClass.newInstance();
        } catch (InstantiationException e1) {
            // fall-through
        } catch (IllegalAccessException e1) {
            assert false;
        }
        if (raider != null) {
            synchronized (mModel) {
                raider.name(name);
                int modelRow = mModel.getRowCount();
                try {
                    mModel.setRowAt(raider, modelRow);
                    mApp.postStatusMessage(MessageFormat.format(ADD_STATUS,
                            raider.name()
                    )
                    );
                    fireRosterChanged(raider, RosterChangeType.ADD);
                } catch (RosterTableModel.IdentityCollisionException e1) {
                    JOptionPane.showMessageDialog(mApp,
                            raider.name() + " is already in the list.",
                            "Add canceled",
                            JOptionPane.ERROR_MESSAGE
                    );
                }
            }
        }
    }
}

private static final String NAME = "Add Raider ...";
private static final String SHORT_TEXT = "Add a new entry to the table";
private static final String ADD_STATUS = "Added ''{0}''.";

private final SamsaraFrame mApp;
private final RosterTableModel<E> mModel;
private final Class<E> mElementClass;
}
